package com.example.demo.mcp.webclient.controller;

import io.modelcontextprotocol.client.McpAsyncClient;
import io.modelcontextprotocol.client.McpClient;
import io.modelcontextprotocol.client.McpSyncClient;
import io.modelcontextprotocol.spec.McpSchema;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zwg
 * @date 2025/10/30 17:41
 */
@Slf4j
@RestController
@RequestMapping("/mcp")
@Tag(name = "MCP接口")
public class McpController {

    private final List<McpSyncClient> mcpSyncClients;

    private final List<McpAsyncClient> mcpAsyncClients;

    public McpController(List<McpSyncClient> mcpSyncClients, List<McpAsyncClient> mcpAsyncClients) {
        this.mcpSyncClients = mcpSyncClients;
        this.mcpAsyncClients = mcpAsyncClients;
    }




    @GetMapping(path = "/client")
    @Operation(summary = "获取接入的MCP服务列表")
    public List<Map<String, Object>> clients(@Parameter(description = "类型：sync/async") @RequestParam(defaultValue = "sync") String type){
        List<Map<String, Object>> result = new ArrayList<>();
        try {
            if("sync".equals(type)){
                for (int i = 0; i < mcpSyncClients.size(); i++) {
                    McpSyncClient client = mcpSyncClients.get(i);
                    Map<String, Object> map = Map.of(
                            "name",client.getClientInfo().name(),
                            "version",client.getClientInfo().version(),
                            "toolSize",client.listTools().tools().size()
                    );
                    result.add(map);
                }
            }else if("async".equals(type)){
                for (int i = 0; i < mcpAsyncClients.size(); i++) {
                    McpAsyncClient client = mcpAsyncClients.get(i);
                    Map<String, Object> map = Map.of(
                            "name",client.getClientInfo().name(),
                            "version",client.getClientInfo().version(),
                            "toolSize",client.listTools().block().tools().size()
                    );
                    result.add(map);
                }
            }
        }catch (Exception e) {
            log.error("获取MCP服务列表错误",e);
        }

        return result;
    }


    @GetMapping(path = "/tools/{clientName}")
    @Operation(summary = "获取接入MCP服务的工具列表")
    public List<Map<String, Object>> tools(@PathVariable String clientName){
        List<Map<String, Object>> result = new ArrayList<>();
        try {
                for (int i = 0; i < mcpSyncClients.size(); i++) {
                    McpSyncClient client = mcpSyncClients.get(i);
                    if(clientName.equals(client.getClientInfo().name())){
                        client.listTools().tools().forEach(tool -> {
                            Map<String, Object> map = Map.of(
                                    "name",tool.name(),
                                    "description",tool.description(),
                                    "inputSchema",tool.inputSchema().properties()
                            );
                            result.add(map);
                        });
                        break;
                    }

                }

                for (int i = 0; i < mcpAsyncClients.size(); i++) {
                    McpAsyncClient client = mcpAsyncClients.get(i);
                    if(clientName.equals(client.getClientInfo().name())){
                        client.listTools().block().tools().forEach(tool -> {
                            Map<String, Object> map = Map.of(
                                    "name",tool.name(),
                                    "description",tool.description(),
                                    "inputSchema",tool.inputSchema().properties()
                            );
                            result.add(map);
                        });
                        break;
                    }
                }
        }catch (Exception e) {
            log.error("获取MCP服务列表错误",e);
        }

        return result;
    }
}
